﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace CodeStash.Addin.Core
{
    public sealed class NotifyDataErrorInfoEventManager : WeakEventManager
    {
        static NotifyDataErrorInfoEventManager()
        {
            Manager = new NotifyDataErrorInfoEventManager();
            WeakEventManager.SetCurrentManager(typeof(NotifyDataErrorInfoEventManager), Manager);
        }

        private static NotifyDataErrorInfoEventManager Manager
        {
            get;
            set;
        }

        protected override void StartListening(object source)
        {
            INotifyDataErrorInfo notifyDataErrorInfo = source as INotifyDataErrorInfo;

            if (notifyDataErrorInfo != null)
            {
                notifyDataErrorInfo.ErrorsChanged += ErrorsChanged;
            }
        }

        protected override void StopListening(object source)
        {
            INotifyDataErrorInfo notifyDataErrorInfo = source as INotifyDataErrorInfo;

            if (notifyDataErrorInfo != null)
            {
                notifyDataErrorInfo.ErrorsChanged -= ErrorsChanged;
            }
        }

        private void ErrorsChanged(object sender, DataErrorsChangedEventArgs e)
        {
            DeliverEvent(sender, e);
        }


        public static void AddListener(INotifyDataErrorInfo source, IWeakEventListener listener)
        {
            Manager.ProtectedAddListener(source, listener);
        }

        public static void RemoveListener(INotifyDataErrorInfo source, IWeakEventListener listener)
        {
            Manager.ProtectedRemoveListener(source, listener);
        }

    }
}
